<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <div slot="header" class="clearfix">
        <span>{{ isEditing ? '修改教师信息' : '教师信息' }}</span>
      </div>
      <div v-if="!isEditing" class="profile-info">
        <div class="info-item">
          <span class="label">ID:</span>
          <span>{{ teacher.pkUid }}</span>
        </div>
        <div class="info-item">
          <span class="label">用户名:</span>
          <span>{{ teacher.userName }}</span>
        </div>
        <div class="info-item">
          <span class="label">姓名:</span>
          <span>{{ teacher.realName }}</span>
        </div>
        <div class="info-item">
          <span class="label">性别:</span>
          <span>{{ teacher.sex }}</span>
        </div>
        <div class="info-item">
          <span class="label">联系电话:</span>
          <span>{{ teacher.phone }}</span>
        </div>
        <div class="info-item">
          <span class="label">地址:</span>
          <span>{{ teacher.address }}</span>
        </div>
      </div>
      <el-form v-else :model="editForm" :rules="rules" ref="editForm" label-width="80px" class="profile-form">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="editForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="editForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="editForm.sex" placeholder="请选择性别">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div class="edit-button">
        <el-button type="primary" @click="isEditing ? saveProfile() : editProfile()">{{ isEditing ? '保存' : '修改' }}</el-button>
        <el-button v-if="isEditing" @click="cancelEdit">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: "Profile",
  data() {
    return {
      teacher: {},
      isEditing: false,
      editForm: {
        userName: '',
        realName: '',
        gender: '',
        phone: '',
        address: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ]
      }
    };
  },
  created(){
    this.fetchTeacherData()
  },
  methods: {
    fetchTeacherData(){
      let userId = localStorage.getItem("userId")
      axios.get("/api/user/getById?userId="+userId).then(response =>{
        this.teacher = response.data
      }).catch(err => {
        this.$message.error("教师信息获取失败")
      })
    },
    editProfile() {
      this.isEditing = true;
      this.editForm = { ...this.teacher };
    },
    saveProfile() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          axios.post("/api/teacher/update",this.editForm).then(response => {
            this.teacher = { ...this.editForm };
            this.isEditing = false;
            this.$message.success('信息修改成功');
          }).catch(err => {
            this.$message.error(err.response.data)
          })


          
        } else {
          this.$message.error('请正确填写信息');
        }
      });
    },
    cancelEdit() {
      this.isEditing = false;
    }
  }
};
</script>

<style scoped>
.profile-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 70vh;
}

.profile-card {
  width: 400px;
  padding: 20px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.profile-info {
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  margin-bottom: 10px;
}

.label {
  font-weight: bold;
  width: 80px;
}

.edit-button {
  display: flex;
  justify-content: flex-end;
}

.profile-form {
  margin-bottom: 20px;
}
</style>
